﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAuthorBusinessLayer;
using AlexandriaPressAuthorObject;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm9 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BusinessAuthor bus = new BusinessAuthor();
            if (!Page.IsPostBack)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (bus.validateUserIfAuthor(bus.getAuthorID(HttpContext.Current.User.Identity.Name)))
                    {
                        if (Request.QueryString["id"] != null)
                        {
                            Book book = new Book();
                            book = bus.viewMyBook(Convert.ToInt32(Request.QueryString["id"]), bus.getAuthorID(HttpContext.Current.User.Identity.Name));


                            GridView1.DataSource = bus.getListOfOrderByGenre(bus.getAuthorID(HttpContext.Current.User.Identity.Name), book.Genre, book.Title);
                            GridView1.EmptyDataText = "No Sale For This Book";
                            GridView1.DataBind();
                            lblGetTotalSale.Text = bus.getSaleReporTByGenreOrTitle(bus.getAuthorID(HttpContext.Current.User.Identity.Name), book.Title, txtSaleGenre.Text).ToString();

                            Label12.Visible = false;
                            drpdwnTitleorGenre.Visible = false;
                            lblSaleByGenre.Visible = false;
                            butGetSaleGenre.Visible = false;
                            txtSaleGenre.Visible = false;
                            Label1.Visible = false;
                            lblGetTotalSale.Visible = false ;
                            RequiredFieldValidator1.Visible = false;
                            GridView1.Visible = true;

                        }
                        else
                        {
                            Label12.Visible = false;
                            drpdwnTitleorGenre.Visible = false;
                            lblSaleByGenre.Visible = false;
                            butGetSaleGenre.Visible = false;
                            txtSaleGenre.Visible = false;
                            Label1.Visible = false;
                            lblGetTotalSale.Visible = false;
                            RequiredFieldValidator1.Visible = false;
                            GridView1.Visible = false;
                        }
                    }
                    else
                    {
                        Response.Redirect("~/MasterPages/UserPages/LoggedInUserPage.aspx");
                    }

                }
                else
                {
                    Response.Redirect("~/MasterPages/MainPage.aspx");
                }
            }
            

        }


        protected void butGetSaleGenre_Click(object sender, EventArgs e)
        {
            lblSaleByGenre.Visible = true;
            drpdwnTitleorGenre.Visible = true;
            butGetSaleGenre.Visible = true;
            txtSaleGenre.Visible = true;
            Label12.Visible = true;
            GridView1.Visible = true;
            Label1.Visible = true;
            lblGetTotalSale.Visible = true;
            BusinessAuthor bus = new BusinessAuthor();

            List<Order> lstofOrder = bus.getListOfOrderByGenre(bus.getAuthorID(HttpContext.Current.User.Identity.Name), txtSaleGenre.Text, drpdwnTitleorGenre.SelectedValue);
            GridView1.DataSource = bus.createSaleTable(lstofOrder);
            GridView1.DataBind();
            lblGetTotalSale.Text = bus.getSaleReporTByGenreOrTitle(bus.getAuthorID(HttpContext.Current.User.Identity.Name), drpdwnTitleorGenre.SelectedValue, txtSaleGenre.Text).ToString() + "Php";
            Session["lstOfOrder"] = lstofOrder;
            
        }

        protected void butGetTotalSale_Click(object sender, EventArgs e)
        {
            BusinessAuthor bus = new BusinessAuthor();
            Label12.Visible = false;
            drpdwnTitleorGenre.Visible = false;
            lblSaleByGenre.Visible = false;
            butGetSaleGenre.Visible = false;
            txtSaleGenre.Visible = false;
            GridView1.Visible = true;
            Label1.Visible = true;
            lblGetTotalSale.Visible = true;
            RequiredFieldValidator1.Visible = false;

            GridView1.DataSource = bus.createSaleTable(bus.getListOfOverallSale(bus.getAuthorID(HttpContext.Current.User.Identity.Name)));
            GridView1.DataBind();
            lblGetTotalSale.Text = bus.getOverAllSale(bus.getAuthorID(HttpContext.Current.User.Identity.Name)).ToString() + "Php";
            Session["lstOfOrder"] = bus.getListOfOverallSale(bus.getAuthorID(HttpContext.Current.User.Identity.Name));
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            lblSaleByGenre.Visible = true;
            drpdwnTitleorGenre.Visible = true;
            butGetSaleGenre.Visible = true;
            txtSaleGenre.Visible = true;
            Label12.Visible = true;
            GridView1.Visible = false;
            Label1.Visible = false;
            lblGetTotalSale.Visible = false;
            RequiredFieldValidator1.Visible = true;
            BusinessAuthor bus = new BusinessAuthor();


            List<string> lstofString = bus.populateTitleOrGenreDrpDwn();
            foreach (string type in lstofString)
            {
                drpdwnTitleorGenre.Items.Add(type);
            }
            
        }
        protected void searchGridView1_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BusinessAuthor business = new BusinessAuthor();
            List<Order> lstOfOrder = (List<Order>)Session["lstOfOrder"];
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataSource = business.createSaleTable(lstOfOrder);
            GridView1.DataBind();
        }

    }
}
